﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SX.Utils.Castle.DynamicProxy;
using System.Dynamic;

namespace SX.Utils.Aop
{
    public static class AopFactory
    {
        private static ProxyGenerator generator;
        private static PersistentProxyBuilder builder;
        static readonly ConcurrentDictionary<Type, object> _factoryDictionary = new ConcurrentDictionary<Type, object>();
        static AopFactory()
        {
            builder = new PersistentProxyBuilder();
            generator = new ProxyGenerator(builder);
        }

        public static T GetObject<T>() where T : class
        {
            return generator.CreateClassProxy<T>();
        }
        /// <summary>
        /// 获取DAO层对象
        /// </summary>
        /// <typeparam name="T">Dao层接口</typeparam>
        /// <returns>T.</returns>
        public static T GetDao<T>() where T : class
        {
            return _factoryDictionary.GetOrAdd(typeof(T),
                (type) => generator.CreateInterfaceProxyWithoutTarget(type, new DaoInterceptor(type))) as T;
        } 
    }
}
